package org.sylfra.idea.plugins.revu.ui.forms;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.sylfra.idea.plugins.revu.model.IRevuHistoryHolderEntity;
import org.sylfra.idea.plugins.revu.model.User;

import javax.swing.*;
import java.awt.*;
import java.text.DateFormat;
import java.util.ResourceBundle;

/**
 * @author <a href="mailto:sylfradev@yahoo.fr">Sylvain FRANCOIS</a>
 * @version $Id: HistoryForm.java 16 2008-12-15 20:06:50Z syllant $
 */
public class HistoryForm<T extends IRevuHistoryHolderEntity<T>> extends AbstractUpdatableForm<T>
{
  public static final DateFormat DATE_FORMATTER = DateFormat.getDateTimeInstance(
    DateFormat.LONG, DateFormat.LONG);

  private JPanel contentPane;
  private JLabel lbCreatedBy;
  private JLabel lbCreatedOn;
  private JLabel lbLastUpdatedBy;
  private JLabel lbLastUpdatedOn;

  @NotNull
  public JPanel getContentPane()
  {
    return contentPane;
  }

  protected void internalUpdateUI(T data, boolean requestFocus)
  {
    lbCreatedBy.setText((data == null) ? "" : getUserName(data.getHistory().getCreatedBy()));
    lbLastUpdatedBy.setText((data == null) ? "" : getUserName(data.getHistory().getLastUpdatedBy()));

    synchronized (DATE_FORMATTER)
    {
      lbCreatedOn.setText(((data == null) || (data.getHistory().getCreatedOn() == null))
        ? "" : DATE_FORMATTER.format(data.getHistory().getCreatedOn()));
      lbLastUpdatedOn.setText(((data == null) || (data.getHistory().getLastUpdatedOn() == null))
        ? "" : DATE_FORMATTER.format(data.getHistory().getLastUpdatedOn()));
    }
  }

  private String getUserName(@Nullable User user)
  {
    return (user == null) ? "" : (user.getDisplayName() == null) ? user.getLogin() : user.getDisplayName();
  }

  public JComponent getPreferredFocusedComponent()
  {
    return null;
  }

  public boolean isModified(@NotNull T data)
  {
    return false;
  }

  @Override
  protected void internalUpdateWriteAccess(@Nullable User user)
  {
  }

  public void internalValidateInput()
  {
  }

  protected void internalUpdateData(@Nullable T data)
  {
  }

  {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
    $$$setupUI$$$();
  }

  /**
   * Method generated by IntelliJ IDEA GUI Designer
   * >>> IMPORTANT!! <<<
   * DO NOT edit this method OR call it in your code!
   *
   * @noinspection ALL
   */
  private void $$$setupUI$$$()
  {
    contentPane = new JPanel();
    contentPane.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
    final JPanel panel1 = new JPanel();
    panel1.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1));
    contentPane.add(panel1,
      new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL,
        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
        null, new Dimension(140, 11), null, 0, false));
    panel1.setBorder(BorderFactory.createTitledBorder(
      ResourceBundle.getBundle("org/sylfra/idea/plugins/revu/resources/Bundle").getString(
        "issueForm.history.creation.title")));
    final JLabel label1 = new JLabel();
    this.$$$loadLabelText$$$(label1,
      ResourceBundle.getBundle("org/sylfra/idea/plugins/revu/resources/Bundle").getString("issueForm.history.by.text"));
    panel1.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 1, false));
    lbCreatedBy = new JLabel();
    lbCreatedBy.setEnabled(false);
    lbCreatedBy.setFont(
      new Font(lbCreatedBy.getFont().getName(), lbCreatedBy.getFont().getStyle(), lbCreatedBy.getFont().getSize()));
    lbCreatedBy.setForeground(new Color(-10066330));
    lbCreatedBy.setText("...");
    panel1.add(lbCreatedBy, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    final JLabel label2 = new JLabel();
    this.$$$loadLabelText$$$(label2,
      ResourceBundle.getBundle("org/sylfra/idea/plugins/revu/resources/Bundle").getString("issueForm.history.on.text"));
    panel1.add(label2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 1, false));
    lbCreatedOn = new JLabel();
    lbCreatedOn.setEnabled(false);
    lbCreatedOn.setFont(
      new Font(lbCreatedOn.getFont().getName(), lbCreatedOn.getFont().getStyle(), lbCreatedOn.getFont().getSize()));
    lbCreatedOn.setForeground(new Color(-10066330));
    lbCreatedOn.setText("...");
    panel1.add(lbCreatedOn, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    final JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1));
    contentPane.add(panel2,
      new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL,
        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
        null, new Dimension(140, 11), null, 0, false));
    panel2.setBorder(BorderFactory.createTitledBorder(
      ResourceBundle.getBundle("org/sylfra/idea/plugins/revu/resources/Bundle").getString(
        "issueForm.history.lastUpdate.text")));
    final JLabel label3 = new JLabel();
    this.$$$loadLabelText$$$(label3,
      ResourceBundle.getBundle("org/sylfra/idea/plugins/revu/resources/Bundle").getString("issueForm.history.by.text"));
    panel2.add(label3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 1, false));
    lbLastUpdatedBy = new JLabel();
    lbLastUpdatedBy.setEnabled(false);
    lbLastUpdatedBy.setFont(new Font(lbLastUpdatedBy.getFont().getName(), lbLastUpdatedBy.getFont().getStyle(),
      lbLastUpdatedBy.getFont().getSize()));
    lbLastUpdatedBy.setForeground(new Color(-10066330));
    lbLastUpdatedBy.setText("...");
    panel2.add(lbLastUpdatedBy, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    final JLabel label4 = new JLabel();
    this.$$$loadLabelText$$$(label4,
      ResourceBundle.getBundle("org/sylfra/idea/plugins/revu/resources/Bundle").getString("issueForm.history.on.text"));
    panel2.add(label4, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 1, false));
    lbLastUpdatedOn = new JLabel();
    lbLastUpdatedOn.setEnabled(false);
    lbLastUpdatedOn.setFont(new Font(lbLastUpdatedOn.getFont().getName(), lbLastUpdatedOn.getFont().getStyle(),
      lbLastUpdatedOn.getFont().getSize()));
    lbLastUpdatedOn.setForeground(new Color(-10066330));
    lbLastUpdatedOn.setText("...");
    panel2.add(lbLastUpdatedOn, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    final Spacer spacer1 = new Spacer();
    contentPane.add(spacer1,
      new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1,
        GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
    final Spacer spacer2 = new Spacer();
    contentPane.add(spacer2,
      new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
        GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
  }

  /**
   * @noinspection ALL
   */
  private void $$$loadLabelText$$$(JLabel component, String text)
  {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++)
    {
      if (text.charAt(i) == '&')
      {
        i++;
        if (i == text.length())
        {
          break;
        }
        if (!haveMnemonic && text.charAt(i) != '&')
        {
          haveMnemonic = true;
          mnemonic = text.charAt(i);
          mnemonicIndex = result.length();
        }
      }
      result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic)
    {
      component.setDisplayedMnemonic(mnemonic);
      component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
  }

  /**
   * @noinspection ALL
   */
  public JComponent $$$getRootComponent$$$()
  {
    return contentPane;
  }
}
